<?php 

namespace app\admin\service;


use app\admin\model\DeviceQuota;

/**
* 设备额度管理-服务类
* Class DeviceQuotaService
* @package app\admin\service
*/
class DeviceQuotaService extends BaseService
{
    /**
    * 构造函数
    */
    public function __construct()
    {
        $this->model = new DeviceQuota();
    }

    /**
    * 获取数据列表
    */
    public function getList()
    {
        $param = request()->param();

        // 查询条件
        $map = [];
        $sort = 'id desc';
        $is_sql = 0;

        
        //获取数据总数
        $count = $this->model->where($map)->count();
        // 打印SQL
        if ($is_sql) {
            echo $this->model->getLastSql();
        }
        $list = [];
        if ($count > 0) {
            $list = $this->model
            ->where($map)
            ->order($sort)
            ->page(PAGE, PERPAGE)
            ->field('update_time,delete_time', true)
            ->select();
        }

        //返回结果
        return array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );

    }


    public function delete()
    {
        // 参数
        $param = request()->param();
        // 记录ID
        $ids = getter($param, "id");
        if (empty($ids)) {
            return message("记录ID不能为空", false);
        }
        $ids = (array)$ids;

        $result = DeviceQuota::destroy($ids);
        if (!$result) {
            return message("删除失败", false);
        }
        return message("删除成功");
    }


    public function info()
    {
        $param = request()->param();
        $imei = getter($param, "imei");
        if (empty($imei)) {
            return message("imei不能为空", false);
        }
        $info = $this->model->where('imei',$imei)->find();
        return message("操作成功", true, $info);
    }

    public function edit()
    {
        $param = request()->param();
        $imei = getter($param, "imei");
        if (empty($imei)) {
            return message("imei不能为空", false);
        }
        unset($param['id']);
        $model = DeviceQuota::get(['imei' => $imei]);
        if (empty($model)) {
            $result = DeviceQuota::create($param);
        } else {
            $result = $model->save($param);
        }

        if (!$result) {
            return message("失败", false);
        }
        return message("成功");
    }

}

